package com.sunda.spmswms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmswms.entity.SapBoxNote;
import com.sunda.spmswms.mapper.*;
import com.sunda.spmswms.service.ISapBoxNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2021-03-29
 */
@Service
public class SapBoxNoteServiceImpl extends ServiceImpl<SapBoxNoteMapper, SapBoxNote> implements ISapBoxNoteService {
    @Autowired
    SapBoxNoteMapper sapBoxNoteMapper;

    @Autowired
    SapDeliveryNoteMapper sapDeliveryNoteMapper;
    @Autowired
    SapBoxNoteDtlMapper sapBoxNoteDtlMapper;
    @Autowired
    WhsInventoryInfoMapper whsInventoryInfoMapper;
    @Autowired
    WhsStorageInventoryMapper whsStorageInventoryMapper;

    /*
     * 根据箱码，验证箱码信息
     * 1. 从SAP_BOX_NOTE中验证箱码有效
     * 2. 从SAP_BOX_NOTE_DTL中验证箱码有DTL
     * 3. 从WHS_INVENTORY_INFO验证有仓库库存
     * 4. 从WHS_STORAGE_INVENTORY中验证是否有储位库存
     * */
    @Override
    public ResponseResult validateBoxNote(String boxNote,String werks,String whsLoationCode) {
        if(StringUtils.isEmpty(boxNote)){
            return ResponseResult.error("boxNote is mandatory required");
        }
        int sapBoxNote= sapBoxNoteMapper.validateBoxNote(boxNote);
        int sapBoxNoteDtl=sapBoxNoteDtlMapper.validateBoxNote(boxNote);
        int whsInventoryInfo=whsInventoryInfoMapper.validateBoxNote(boxNote,werks,whsLoationCode);
        int whsStorageInventory=whsStorageInventoryMapper.validateBoxNote(boxNote,werks,whsLoationCode);
        return ResponseResult.success().add("sapBoxNote",sapBoxNote)
                .add("sapBoxNoteDtl",sapBoxNoteDtl)
                .add("whsInventoryInfo",whsInventoryInfo)
                .add("whsStorageInventory",whsStorageInventory);
    }

    @Override
    public List<SapBoxNote> getSapBoxNote(String packageNote) {

        if (null==packageNote || "".equals(packageNote)) {
            return new ArrayList<>();
        }
        return sapBoxNoteMapper.selectList(new QueryWrapper<SapBoxNote>().eq("PACKAGE_NOTE",packageNote));
    }

    @Override
    public List<Map<String, Object>> getDnMaterialDtl(String boxNote) {
        if (StringUtils.isEmpty(boxNote)){
            return null;
        }
        return sapBoxNoteMapper.getDnMaterialDtl(boxNote);
    }

    @Override
    public void insertSapBoxNote(SapBoxNote sapBoxNote){
        this.baseMapper.insert(sapBoxNote);
    }

//    查找新箱
    @Override
    public ResponseResult checkNewBox(String boxNote){
        return ResponseResult.success().add("newBoxList",sapBoxNoteMapper.checkNewBox(boxNote));
    }

//    查找新箱List
    @Override
    public List<Map<String, Object>> getNewBox(String boxNote){
        List<Map<String, Object>> maps = sapBoxNoteMapper.checkNewBox(boxNote);
        return maps;
    }

    @Override
    public SapBoxNote getByBoxNote(String boxNote) {
        if (StringUtils.isEmpty(boxNote)){
            return null;
        }
        return this.baseMapper.selectOne(new QueryWrapper<SapBoxNote>().eq("BOX_NOTE", boxNote));
    }

    @Override
    public int getNewBoxNote() {
        return this.baseMapper.getNewBoxNote();
    }

    /** 备件货收货完成提交SAP */
//    @Override
//    public Map<String, Object> getDnDataToSap(String deliveryNoteSap, String postingDate, String userId) {
//        if (StringUtils.isEmpty(deliveryNoteSap)){
//            return null;
//        }
//        try {
//            Map<String, Object> map = new HashMap<>();
//            SapDeliveryNote sapDeliveryNote = sapDeliveryNoteMapper.selectOne(new QueryWrapper<SapDeliveryNote>().eq("DELIVERY_NOTE_SAP", deliveryNoteSap));
//            /** 提交前交货单状态需为 6 - 已验收待上传SAP；不为 6 的返回空。 提交SAP前将 过账日期 - postingDate 字段写入该条数据 */
//            if ("6".equals(sapDeliveryNote.getSpmsStatus())){
//                sapDeliveryNote.setPostingDate(postingDate);
//                int resp = sapDeliveryNoteMapper.update(sapDeliveryNote, new QueryWrapper<SapDeliveryNote>().eq("DELIVERY_NOTE_SAP", deliveryNoteSap));
//                if (resp == 1){
//                    /** 备件货交货单状态更新成功后，查询获取箱码及箱码明细数据 */
//                    //List<Map<String, Object>> sapBoxNoteDtlList = this.baseMapper.getBoxNoteDtlToSap(deliveryNoteSap);
//                    List<Map<String, Object>> sapBoxNoteDtlList = new ArrayList<>();
//                    Map<String, Object> Head = new HashMap<>();
//                    Head.put("xblnr", StringUtils.isNotEmpty(sapDeliveryNote.getDeliveryNoteSrm()) ? sapDeliveryNote.getDeliveryNoteSrm() : "");
//                    Head.put("vbelnIm", StringUtils.isNotEmpty(sapDeliveryNote.getDeliveryNoteSap()) ? sapDeliveryNote.getDeliveryNoteSap() : "");
//                    Head.put("bldat", StringUtils.isNotEmpty(sapDeliveryNote.getDocumentDate()) ? sapDeliveryNote.getDocumentDate() : "");
//                    Head.put("budat", postingDate);
//                    Head.put("createdby", StringUtils.isNotEmpty(sapDeliveryNote.getUserId()) ? sapDeliveryNote.getUserId() : "");
//                    //移动类型采购入库 101；免费入库511；
//                    Head.put("bwart", StringUtils.isNotEmpty(sapDeliveryNote.getMobileType()) ? sapDeliveryNote.getMobileType() : "101");
//                    Head.put("bktxt", StringUtils.isNotEmpty(sapDeliveryNote.getRemarks()) ? sapDeliveryNote.getRemarks() : "");
//
//                    List<Map<String, Object>> Items = new ArrayList<>();
//                    for (Map<String, Object> sapDeliveryNoteDtl : sapBoxNoteDtlList){
//                        Map<String, Object> Item = new HashMap<>();
//                        Item.put("vbelnIm", sapDeliveryNoteDtl.get("deliveryNoteSap").toString());
//                        Item.put("vbelpIm", sapDeliveryNoteDtl.get("deliveryNoteSapItem").toString());
//                        Item.put("matnr", sapDeliveryNoteDtl.get("materialNo").toString());
//                        Item.put("bstmg", sapDeliveryNoteDtl.get("domesticQtyReceived").toString());
//                        Item.put("bstme", sapDeliveryNoteDtl.get("deliveryUnit").toString());
//                        Item.put("werks", sapDeliveryNoteDtl.get("werks").toString());
//                        Item.put("lgort", sapDeliveryNoteDtl.get("whsLocationCode").toString());
//                        /** sgtxt - 项目文本。备件按箱存储，交货单一行可能跨越多个箱子，无法留存备注信息。 */
//                        Item.put("sgtxt", "");
//                        Items.add(Item);
//                    }
//                    map.put("Head", Head);
//                    map.put("Items", Items);
//                    return Z_SPMS_MIGO(map);
//                }
//            }
//            return null;
//        }catch (Exception e){
//            e.printStackTrace();
//            return null;
//        }
//    }
//
//    public Map<String, Object> Z_SPMS_MIGO(Map<String, Object> map) {
//        String mqUuid = UUID.randomUUID().toString();
//        JSONObject doc = new JSONObject();
//        JSONObject REQUEST = new JSONObject();
//        JSONObject ESB_ATTRS = new JSONObject();
//        JSONObject REQUEST_DATA = new JSONObject();
//
//        ESB_ATTRS.put("Application_ID", "00020000000002");
//        ESB_ATTRS.put("Transaction_ID", mqUuid);
//        ESB_ATTRS.put("App_ID", "SPMS");
//
//        REQUEST_DATA.put("Operation", "Z_SPMS_MIGO");
//        REQUEST_DATA.put("Type", "SPMS_MIGO");
//        REQUEST_DATA.put("Head", map.get("Head"));
//        REQUEST_DATA.put("Items", map.get("Items"));
//
//        REQUEST.put("ESB_ATTRS", ESB_ATTRS);
//        REQUEST.put("REQUEST_DATA", REQUEST_DATA);
//
//        doc.put("REQUEST", REQUEST);
//        return doc;
//    }
}
